package cn.edu.nju.setting;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import cn.edu.nju.R;
import cn.edu.nju.storage.StoreDataProvider;

import android.R.integer;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

public class FilterRule {
	private String ruleName;
	private boolean isApplied;
	private List<String> tags;
	private long id;
	private Activity activity;
	private String serverUrl;

	public FilterRule(String ruleName, boolean isApplied, List<String> tags,
			long id, Activity activity) {
		this.ruleName = ruleName;
		this.isApplied = isApplied;
		this.tags = tags;
		this.id = id;

		this.activity = activity;
		this.serverUrl = activity.getString(R.string.server_url) + "rules.php";
	}

	public FilterRule(Activity activity) {
		this.ruleName = null;
		this.isApplied = false;
		this.tags = new ArrayList<String>();
		this.activity = activity;
		this.serverUrl = activity.getString(R.string.server_url) + "rules.php";
	}

	public String getRuleName() {
		return ruleName;
	}

	public boolean isApplied() {
		return isApplied;
	}

	public List<String> getTags() {
		return tags;
	}

	public void toggleApplied() {
		try {
			HttpClient client = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost post = new HttpPost(serverUrl);
			
			Log.v("Tohggle", "" + isApplied);
			MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);

			entity.addPart("option", new StringBody("changeApply"));
			entity.addPart("id", new StringBody("" + id));
			if (isApplied) {
				entity.addPart("isUsed", new StringBody("0"));
			} else {
				entity.addPart("isUsed", new StringBody("1"));
			}

			post.setEntity(entity);

			HttpResponse response = client.execute(post, localContext);
			HttpEntity httpEntity = response.getEntity();
			InputStream input = httpEntity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));

			String context = reader.readLine();
			reader.close();

			if (context.equals(new String("OK"))) {
				isApplied = !isApplied;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void upDate() {
		try {
			HttpClient client = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost post = new HttpPost(serverUrl);

			MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);

			entity.addPart("option", new StringBody("update"));
			entity.addPart("id", new StringBody("" + id));
			entity.addPart("num", new StringBody("" + tags.size()));

			for (int i = 0; i < tags.size(); i++) {
				entity.addPart("tag" + i, new StringBody(tags.get(i),Charset.forName("utf-8")));
			}

			post.setEntity(entity);

			HttpResponse response = client.execute(post, localContext);
			HttpEntity httpEntity = response.getEntity();
			InputStream input = httpEntity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));

			String context = reader.readLine();
			reader.close();

//			Toast.makeText(activity, context, 100000).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
